package com.antg;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author Antg
 * @date 2021/10/20  20:51
 */
public class Code_202 {
    static Set<Integer> set = new HashSet<>();
    public static void main(String[] args) {
        int n = 10;
        System.out.println(isHappy(n));
    }
    public static boolean isHappy(int n) {
        if(set.contains(n)){
            return false;
        }else{
            set.add(n);
        }
        if(n==1){
            return true;
        }
        //首先将数拆分开
            Long sum = getSum(getEveryone(n));
            if(sum>Integer.MAX_VALUE){
                return false;
            }
            if(sum==1){
                return true;
            }else{
                return isHappy(Integer.parseInt(sum.toString()));
            }
    }
    //求和
    public static Long getSum(ArrayList<Long> array){
        Long sum = 0L;
        for(Long ele:array){
            sum+=ele*ele;
        }
        return sum;
    }
    //获取每一位
    public static ArrayList<Long> getEveryone(int n){
        ArrayList<Long> arrayList = new ArrayList<>();
        long a = 10;
        long b = 1;
        int len = String.valueOf(n).length();
        while (len!=0){
            arrayList.add(n%a/b);
            a *=10;
            b *=10;
            len--;
        }
        System.out.println(arrayList);
        return arrayList;

    }
}
